package com.leyou.item.controller;


import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page",defaultValue ="1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy",required = false)String sortBy,
            @RequestParam(value = "desc",required = false)Boolean desc,
            @RequestParam(value = "key",required = false)String key
          ){

                return ResponseEntity.ok( brandService.queryBrandByPage(page,rows,sortBy,desc,key));
    }

    //数据新增
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO ,@RequestParam("cids")List<Long> cids){

           this.brandService.addBrand(brandDTO,cids);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //编辑品牌数据
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO,@RequestParam("cids")List<Long> cids){
            this.brandService.updateBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

     //删除品牌数据
    @DeleteMapping("bid/{bid}")
   public ResponseEntity<Void> deleteBrand(@PathVariable("bid")String bid){
        this.brandService.deleteBrand(Long.parseLong(bid));
        return ResponseEntity.status(HttpStatus.OK).build();
   }



}
